#pragma once

#include <iostream>
#include <mutex>
#include <thread>
#include "utils.hpp"

static thread_local std::ostringstream local_buffer;
static std::mutex s_LogMutex;

#define FORLOOP(cnt) for(int i=0; i<cnt; ++i)

// ANSI color codes
#define RESET   "\033[0m"
#define RED     "\033[31m"
#define GREEN   "\033[32m"
#define YELLOW  "\033[33m"
#define BLUE    "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN    "\033[36m"

#define LOG(...) { \
    local_buffer << RESET << MAGENTA << jizhe::GetCurrentTime() <<" " << RED << jizhe::GetCurrentFile(__FILE__) << ":" << __LINE__ << BLUE << " {thread_" << jizhe::GetThreadID() << "} " << GREEN << __VA_ARGS__ << RESET << std::endl; \
    std::unique_lock<std::mutex> lock(s_LogMutex); \
    std::cout << local_buffer.str();\
}


#define LOG_CO(...) { \
    local_buffer << RESET << MAGENTA << jizhe::GetCurrentTime() <<" " << RED << jizhe::GetCurrentFile(__FILE__) << ":" << __LINE__ << BLUE << " {thread_" << jizhe::GetThreadID() << "} " << YELLOW << "{co_" << jizhe::GetRunningCoID() << "} " << GREEN << __VA_ARGS__ << RESET << std::endl; \
    std::unique_lock<std::mutex> lock(s_LogMutex); \
    std::cout << local_buffer.str();\
}